package myQueue;

import java.util.Stack;

class MyQueue8 {
    //stack1用于放元素
    //stack2用于出元素

    Stack<Integer> stack1;
    Stack<Integer> stack2;

    public MyQueue8() {
        stack1=new Stack<>();
        stack2=new Stack<>();
    }

    public void push(int x) {
        int a;
        //如果stack2为空则直接往stack1中添加元素
        //否则要先将stack2中的元素全部转移到stack1中再进行元素放入
        if(stack2.empty()){
            stack1.push(x);
        }else{
            a=stack2.size();
            while(a>0){
                stack1.push(stack2.pop());
                a--;
            }
            stack1.push(x);
        }
    }

    public int pop() {
        int a=stack1.size();
        int p;
        while(a>0){
            stack2.push(stack1.pop());
            a--;
        }
        p=stack2.pop();
        a=stack2.size();
        while(a>0){
            stack1.push(stack2.pop());
            a--;
        }
        return p;

    }

    public int peek() {
        int a=stack1.size();
        int p;
        while(a>0){
            stack2.push(stack1.pop());
            a--;
        }
        p=stack2.peek();
        a=stack2.size();
        while(a>0){
            stack1.push(stack2.pop());
            a--;
        }
        return p;

    }

    public boolean empty() {
        if(stack1.empty()&&stack2.empty()){
            return true;
        }
        return false;
    }
}
public class Test2 {
    public static void main(String[] args) {
        MyQueue8 queue8=new MyQueue8();
        queue8.push(1);
        queue8.push(2);
        queue8.push(3);
        System.out.println(queue8.peek());
        System.out.println(queue8.pop());
        System.out.println(queue8.peek());

    }
}
